import { useState } from 'react';
import styles from './index.module.less';
import { Button, Form, Input, message } from 'antd';
import { addUser } from '../../services/palaceUser';

const { TextArea } = Input;
const Index = () => {
  const [form] = Form.useForm();
  const [saving, setSaving] = useState(false);

  const handlerSave = () => {
    setSaving(true);
    form.validateFields().then(async (values) => {
      try {
        const res = await addUser(values);
        if (res.data.code === 200) {
          message.success('操作成功');
          form.resetFields();
        } else {
          message.warning(res.data.message);
        }
      } catch (error) {
        console.log(error);
      }
      setSaving(false);
    }, () => {
      setSaving(false);
    });
  };

  return (
    <div className={styles.container}>
      <Form
        style={{ padding: 24 }}
        form={form}
        labelCol={{ span: 6 }}
        wrapperCol={{ span: 16 }}
      >
        <Form.Item
          label="内容"
          name="content"
          rules={[{ required: true, message: '请输入内容' }]}
        >
          <TextArea rows={4} />
        </Form.Item>
        <Form.Item>
          <Button loading={saving} onClick={handlerSave} type="primary">提交</Button>
        </Form.Item>
      </Form>
    </div>
  );
};

export default Index;
